﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebNext.UI.Models.Tags;
using System.Collections.Specialized;
using WebNext.UI.Models.Entries;
using WebNext.Domain.Entities;
using System.Text.RegularExpressions;

namespace WebNext.UI.Utils.ModelBinders
{
	public class EntryEditActionModelBinder : IModelBinder
	{
		public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
		{
			if (bindingContext.Model != null)
			{
				throw new NotImplementedException("Cannot update instances");
			}
			return this.BindEntryEditActionModel(controllerContext.HttpContext.Request);
		}

		private EntryEditAction BindEntryEditActionModel(HttpRequestBase request)
		{
			string[] actionParts = request.Form["action"].Split('_');
			EntryEditActionType actionType = (EntryEditActionType)Enum.Parse(typeof(EntryEditActionType), actionParts[0]);
			EntryElementType elementType = EntryElementType.Header;
			
			int index = 0;

			switch(actionType)
			{
				case EntryEditActionType.AddElement:
					index = Convert.ToInt32(actionParts[1]);
					elementType = (EntryElementType)Enum.Parse(typeof(EntryElementType), actionParts[2]);
					break;
				case EntryEditActionType.DeleteElement:
					index = Convert.ToInt32(actionParts[1]);
					break;
			}
			return new EntryEditAction(actionType, index, elementType);
		}
	}
}